package com.leetcode.根据算法进行分类.字符串查找算法相关;

import java.util.HashMap;
import java.util.LinkedList;

/**
 * @author: xiaomi
 * @date: 2021/4/20
 * @description: 28. 实现 strStr()
 */
public class A_28_实现_strStr {
    static A_28_实现_strStr action = new A_28_实现_strStr();

    public static void main(String[] args) {
        test1();
        test2();
        test3();
    }

    static void test1() {
        //0
        String h = "aaa";
        String n = "aaa";
        int res = action.strStr(h, n);
        System.out.println("res = " + res);
    }

    static void test2() {
        //2
        String h = "hello";
        String n = "ll";
        int res = action.strStr(h, n);
        System.out.println("res = " + res);
    }

    static void test3() {
        //

        //System.out.println("res = " + res);
    }


    /**
     * map(key-char;value-stack of index)
     *
     * @param haystack
     * @param needle
     * @return
     */
    public int strStrFail(String haystack, String needle) {
        if ("".equals(needle)) {
            return 0;
        }
        if ("".equals(haystack)) {
            return -1;
        }
        char[] chars = haystack.toCharArray();
        HashMap<Character, LinkedList<Integer>> map = new HashMap<>();
        int len = chars.length;
        for (int i = 0; i < len; i++) {
            Character cha = chars[i];
            if (map.containsKey(cha)) {
                map.get(cha).addLast(i);
            } else {
                LinkedList<Integer> tempList = new LinkedList<>();
                tempList.addLast(i);
                map.put(cha, tempList);
            }
        }
        //
        char[] childChars = needle.toCharArray();
        int cLen = childChars.length;
        Character firstChar = childChars[0];
        while (map.containsKey(firstChar)) {
            LinkedList<Integer> tempList = map.get(firstChar);
            Integer firstIndex = tempList.removeFirst();
            int index = firstIndex;
            int i = 1;
            for (; i < cLen; i++) {
                Character tempC = childChars[i];
                if (!map.containsKey(tempC)) {
                    return -1;
                }
                LinkedList<Integer> ll = map.get(tempC);
                while (!ll.isEmpty() && ll.peekFirst() < index) {
                    ll.removeFirst();
                }
                if (ll.isEmpty()) {
                    map.remove(tempC);
                }
                index++;
                if (!ll.peekFirst().equals(index)) {
                    break;
                } else {
                    //此处不能删除，说明这个思路有问题！
                }
            }
            if (i == cLen - 1) {
                //
                return firstIndex;
            }
            if (tempList.isEmpty()) {
                map.remove(firstChar);
            }
        }
        return -1;
    }

    public int strStr(String haystack, String needle) {
        if ("".equals(needle)) {
            return 0;
        }
        if ("".equals(haystack)) {
            return -1;
        }
        char[] chars = haystack.toCharArray();
        int len = chars.length;
        char[] childChars = needle.toCharArray();
        int cLen = childChars.length;
        Character firstChar = childChars[0];
        LinkedList<Integer> tempList = new LinkedList<>();
        int size = len - cLen;
        if (size < 0) {
            return -1;
        }
        for (int i = 0; i <= size; i++) {
            if (firstChar.equals(chars[i])) {
                tempList.addLast(i);
            }
        }
        while (!tempList.isEmpty()) {
            Integer index = tempList.removeFirst();
            boolean find = true;
            for (int i = 1; i < cLen; i++) {
                if (chars[index + i] != childChars[i]) {
                    find = false;
                    break;
                }
            }
            if (find) {
                return index;
            }
        }
        return -1;
    }
}
